package cn.elwy.rcp.ui.table.adapter;

import cn.elwy.common.util.ReflectUtil;
import cn.elwy.rcp.ui.table.format.ColumnFormat;
import cn.elwy.rcp.ui.table.model.ColumnInfo;
import cn.elwy.rcp.ui.table.model.TableInfo;

public class TreeNodeLabelAdapterImpl implements TreeNodeLabelAdapter {

  @Override
  public Object getText(Object element) {
    if (element != null) {
      return element.toString();
    }
    return null;
  }

  public Object getColumnText(Object element, int columnIndex, TableInfo tableInfo) {
    if (element == null) {
      return null;
    }
    ColumnInfo columnInfo = tableInfo.getColumnInfo(columnIndex);
    String columnId = columnInfo.getId();
    Object value = null;
    if (tableInfo.isTable()) {
      value = ReflectUtil.getTableColumnValue(element, columnId, true);
    } else {
      value = ReflectUtil.getTreeColumnValue(element, columnId, true);
    }
    ColumnFormat format = columnInfo.getColumnFormat();
    if (format != null && value != null) {
      value = format.format(value);
    }
    return value;
  }

}
